package com.wwwarehouse.common.tools;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;

import com.wwwarehouse.common.BuildConfig;
import com.wwwarehouse.common.custom_widget.time_pick.DateUtil;

import java.io.File;

/**
 * @author : mundane
 * @time : 2017/8/22 10:02
 * @description :
 * @file : TakePhotoHelper.java
 */

public class TakePhotoHelper {


	private final FileUtils mFileUtils;
	private Fragment mFragment;
	private int mRequestCode;
	private File mPhotoFile;
	private Uri mUri;

	public TakePhotoHelper(Fragment fragment, int requestCode) {
		mFragment = fragment;
		mRequestCode = requestCode;
		mFileUtils = new FileUtils();
	}

	public File getPhotoFile() {
		return mPhotoFile;
	}

	/**
	 * 拍照
	 */
	public void capture() {
		mPhotoFile = mFileUtils.getImagesFile("IMG_" + DateUtil.formatDate(System.currentTimeMillis(), "yyyyMMdd_HHmmss") + ".jpg");
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			mUri = FileProvider.getUriForFile(mFragment.getContext(), BuildConfig.APPLICATION_ID + BuildConfig.ProviderSuffix, mPhotoFile);
			intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		} else {
			mUri = Uri.fromFile(mPhotoFile);
		}
		intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
		mFragment.startActivityForResult(intent, mRequestCode);
	}

	public void compressPhoto() {
		ImageCompressUtils.compressPhoto(mFragment.getContext(), mUri, mPhotoFile.getPath());
	}

}
